#include "kuozhangtablewidget.h"
#include "QHeaderView"
#include "setting.h"
#include "QPushButton"
#include "QComboBox"
#include "utils/citymanager.h"

KuoZhangTableWidget::KuoZhangTableWidget(QWidget *parent) : QTableWidget(parent)
{

}


void KuoZhangTableWidget::init(){
    QStringList list;
    list<< "序号" <<"本帮城市" << "本帮校尉坐标" << "本帮城市索引" << "任务次数" << "本帮最近城市" << "操作";

    QStringList tip;
    tip<< "序号" <<"本帮校尉城市所在" << "本帮校尉坐标(英文逗号分隔)"<< "本帮城市在校尉对话框中的第几个" << "任务次数(一个来回为1次)" << "距离本帮最近的三大城市" << "操作";
    setHorizontalHeaderLabels(list);
    setFocusPolicy(Qt::FocusPolicy::NoFocus);
    QHeaderView *headerView = horizontalHeader();
    headerView->setSectionResizeMode(QHeaderView::Stretch); //自适应列宽
    headerView->setMinimumHeight(40);
    headerView->setStyleSheet("QHeaderView::section {background-color: #ededed; border-bottom:1px solid #000000}");
    headerView->setSelectionBehavior(QAbstractItemView::SelectionBehavior::SelectRows);
    headerView->setHighlightSections(false);

    for(int i = 0 ; i < list.size(); i++){
        QTableWidgetItem *item = new QTableWidgetItem(list.at(i));
        QFont font = this->font();
        font.setBold(true);
        item->setFont(font);
        item->setFlags(Qt::ItemFlag::NoItemFlags);
        item->setSelected(false);
        item->setBackground(Qt::gray);
        item->setToolTip(tip.at(i));
        if(i != list.size() -1){
            item->setForeground(Qt::red);
        }
        setHorizontalHeaderItem(i, item);

    }
    QList<KuoZhangBean> kuoZhangBeans = readKuoZhangByFile();
    if(!kuoZhangBeans.isEmpty()){
        for(KuoZhangBean kuoZhangBean : kuoZhangBeans){
            QString groupXiaoWeiLocationX= kuoZhangBean.groupXiaoWeiLocation;
            creatNewRow(kuoZhangBean.num, kuoZhangBean.groupCity, groupXiaoWeiLocationX, kuoZhangBean.groupIndex, kuoZhangBean.taskTotal,kuoZhangBean.shortCityName);
        }
    }
}


QList<KuoZhangBean> KuoZhangTableWidget::readKuoZhangByFile()
{
    QString json = Setting::getInstance()->getKuoZhangJson();
    return KuoZhangBean::jsonToArray(json.toUtf8());
}

bool KuoZhangTableWidget::saveBeans2File(){
    QList<KuoZhangBean *> allKuoZhangBeans;
    if(getAllTableBeans(&allKuoZhangBeans)){
        QByteArray a= KuoZhangBean::ArraytoJson(&allKuoZhangBeans);
        Setting::getInstance()->setKuoZhangJson(QString(a));
        return true;
    }
    qDeleteAll(allKuoZhangBeans.begin(), allKuoZhangBeans.end());
    allKuoZhangBeans.clear();
    return false;
}

int KuoZhangTableWidget::creatNewRow(int num, QString groupCity, QString groupXiaoWeiLocation, int groupIndex, int taskTotal, QString shortCityName)
{
    if(!isCreateTableFinish())return -1;
    int row = rowCount();
    setRowCount(row  + 1);

    //序号
    QTableWidgetItem *item111 = new QTableWidgetItem();
    item111->setTextAlignment(Qt::AlignCenter);
    item111->setSelected(false);
    item111->setText(num == 0 ? "" :QString::number(num));
    this->setItem(row ,numType, item111);

    //本帮城市
    QTableWidgetItem *item0 = new QTableWidgetItem();
    item0->setTextAlignment(Qt::AlignCenter);
    item0->setSelected(false);
    item0->setText(groupCity);
    this->setItem(row ,groupCityType, item0);

    //本帮校尉坐标
    QTableWidgetItem *item11 = new QTableWidgetItem();
    item11->setTextAlignment(Qt::AlignCenter);
    item11->setSelected(false);
    item11->setText(groupXiaoWeiLocation);
    this->setItem(row ,groupLocationType, item11);

    //本帮城市索引
    QTableWidgetItem *item12 = new QTableWidgetItem();
    item12->setTextAlignment(Qt::AlignCenter);
    item12->setSelected(false);
    item12->setText(groupIndex == 0 ? "" : QString::number(groupIndex));
    this->setItem(row ,groupIndexType, item12);

    //扩张任务总次数
    QTableWidgetItem *item146 = new QTableWidgetItem();
    item146->setTextAlignment(Qt::AlignCenter);
    item146->setSelected(false);
    item146->setText(taskTotal == 0 ? "" :QString::number(taskTotal));
    this->setItem(row ,taskTotalType, item146);

    //距离帮派最近的城市
    QComboBox *box = new QComboBox(this);
    QStringList citys = CityManager::getInstance()->getBigCitys();
    box->addItems(citys);
    box->setCurrentIndex(citys.indexOf(shortCityName));
    box->setMaxCount(5);
    box->setEditable(false);
    this->setCellWidget(row ,shortCityType, box);

    //操作 删除
    QPushButton *deleteButton = new QPushButton(this);
    deleteButton->setText("删除");
    deleteButton->setStyleSheet("color:red");
    setCellWidget(row, deleteType, deleteButton);
    connect(deleteButton, &QPushButton::clicked,this,[&](){
        this->removeRow(currentRow());
    }); // 再连接信号
    return 0;
}

bool KuoZhangTableWidget::isCreateTableFinish(){
    int row = rowCount();
    if(row == 0) return true;
    bool isFinish = true;
    for(int i = 0 ; i< columnCount() - 1; i++){
        if(i == shortCityType) continue;
        QString content = item(row - 1,i)->text();
        isFinish = isFinish && !content.isEmpty();
    }
    return isFinish;
}

bool KuoZhangTableWidget::getBean(int row, KuoZhangBean *kuozhang, bool isNeedFindPath){

    bool isOk = false;
    QString numStr = item(row ,numType)->text();
    int num = numStr.toInt(&isOk);
    if(!isOk){
        emit logEvent("角色序号设置错误");
        return false;
    }
    kuozhang->num = num;

    QString groupCity = item(row ,groupCityType)->text();
    kuozhang->groupCity = groupCity;


    QComboBox *comboBox = (QComboBox*)cellWidget(row,groupLocationType);
    QString groupXiaoWeiLocation = comboBox->currentText();
    if(!groupXiaoWeiLocation.isEmpty() && groupXiaoWeiLocation != "无"){
        kuozhang->groupXiaoWeiLocation = groupXiaoWeiLocation;
    }else{
         emit logEvent("本帮校尉位置");
        return false;
    }


    QString groupIndexStr = item(row ,groupIndexType)->text();
    int groupIndex = groupIndexStr.toInt(&isOk);
    if(!isOk){
        emit logEvent("本帮城市索引设置错误");
        return false;
    }
    kuozhang->groupIndex = groupIndex;

    QString taskTotalStr = item(row ,taskTotalType)->text();
    int taskTotal = taskTotalStr.toInt(&isOk);
    if(!isOk){
        emit logEvent("任务次数设置错误");
        return false;
    }
    kuozhang->taskTotal = taskTotal;

    QComboBox *shortCityComboBox = (QComboBox*)cellWidget(row,shortCityType);
    kuozhang->shortCityName = shortCityComboBox->currentText();

    if(isNeedFindPath){
        kuozhang->setLine();
    }

    return true;
}

bool KuoZhangTableWidget::getAllTableBeans(QList<KuoZhangBean*> *allKuoZhangBeans, bool isNeedFindPath)
{
    if(allKuoZhangBeans == nullptr){
        return false;
    }
    qDeleteAll(allKuoZhangBeans->begin(), allKuoZhangBeans->end());
    allKuoZhangBeans->clear();
    for(int i = 0 ; i < rowCount() ; i++){
        KuoZhangBean *kuozhangBean = new KuoZhangBean();
        if(getBean(i, kuozhangBean, isNeedFindPath)){
            allKuoZhangBeans->append(kuozhangBean);
        }
    }
    if(allKuoZhangBeans->isEmpty()){
        return false;
    }
    return true;
}
